var timer;
$(document).ready(init());
function init(){
	getAjaxData(1);
	
}
function clearNotify(){
	clearInterval(timer);
	$('#notify_content').show();
}

function notifing(){
	$('#notify_button').fadeOut(500);
	$('#notify_button').fadeIn(500);
}

function getRemind(){
	
	
}

function getAjaxData(page){
	$.ajax( {
		url : "next!next.action",
		type : "POST",
		data : {
			pageNow : page
		},
		dataType : "json",
		success : function(data){
			setIndexContent(data);
		}
	});	
}

function getListContent(){
	$.ajax( {
		url : "next!info.action",
		type : "POST",
		data : {},
		dataType : "json",
		success : function(data){
			setMyListData(data);
		}
	});	
}

function getMessageData(page){
	$.ajax( {
		url : "next!message.action",
		type : "POST",
		data : {
			pageNow:page
		},
		dataType : "json",
		success : function(data){
			setMessageContent(data);
			setMyMessageContent(data);
		}
	});
}

function setIndexContent(data){
	$('#friend_news_content').html('');
	if(data.dynamicInfoMap.length == 0){
		$('#friend_news_content').html('<span style="text-align: center;margin-top: 150px;margin-left: 130px;position: absolute;">初来乍到，还没有任何动态呢，亲！</span>');
	}else{
		$.each(data.dynamicInfoMap, function(key, value) {
		if(value.type == 'comment'){
			$('#friend_news_content').append('<div class="share_content" style="margin-top:15px;margin-left:17px"><a href="/ShareMusicProject/home.action?id='+
			value.sourceId+'" class="f1" title="头像"><img width="56" height="53" alt="" src="'+
			value.sourceImage+'"></a><p><a href="/ShareMusicProject/home.action?id='+value.sourceId+'">'+value.sourceName
			+'</a>评论了音乐<q>'+value.content+'</q></p><div class="bubble bubble_radio2"><a href="javascript:;" title="播放音乐" target="_blank" class="bubble_play"><img onclick="playMusic2(\''+
				value.number+'\',this)" src="my_profile_files/play.png" width="45" height="45" alt="播放电台"></a><a title="" href="/ShareMusicProject/music.action?id='+value.targetId+'" class="radio_cover f1"><img width="100" height="100" src="'+value.targetImage+'" alt=""></a><p><a href="music?id='+value.targetId+'" title="kesha" target="_blank">'+
			value.targetName+'</a></p><p><span>"上传者："<a href="/ShareMusicProject/home.action?id='+value.userId+'"> '+value.username+'</a></span></p></div><p class="minor">'+value.time+'</p>  </div>');
		}else if(value.type == 'music'){
			$('#friend_news_content').append('<div class="share_content" style="margin-top: 15px;margin-left:17px"><a href="/ShareMusicProject/home.action?id='+
				value.sourceId+'" class="f1" title="头像"><img width="56" height="53" alt="" src="'+
				value.sourceImage+'"></a><p><a href="/ShareMusicProject/home.action?id='+value.sourceId+'">'+
				value.sourceName+'</a>上传分享了音乐</p><div class="bubble bubble_radio2"><a href="javascript:;" title="播放音乐" target="_blank" class="bubble_play"><img onclick="playMusic2(\''+
				value.content+'\',this)" src="my_profile_files/play.png" width="45" height="45" alt="播放电台"></a><a title="" href="/ShareMusicProject/music.action?id='+value.targetId+'" class="radio_cover f1"><img width="100" height="100" src="'+value.targetImage+'" alt=""></a><p><a href="/ShareMusicProject/music.action?id='+value.targetId+'" title="kesha" target="_blank">'+
				value.targetName+'</a></p><p><span>"推荐描述：'+value.number+'"</span></p></div><p class="minor">'+value.time+'</p> </div>');
		}else if(value.type == 'follow'){
			$('#friend_news_content').append('<div class="share_content" style="margin-top: 15px;margin-left:17px;height: 63px;"><a href="/ShareMusicProject/home.action?id='+
				value.sourceId+'" class="f1" title="头像"><img width="56" height="53" alt="" src="'+value.sourceImage+'"></a><p style="margin-top: 0px;padding-top: 14px;"><a href="/ShareMusicProject/home.action?id='+
				value.sourceId+'" style="margin-right: 5px;margin-top: 0px;">'+
				value.sourceName+'</a> 关注了<a href="/ShareMusicProject/home.action?id='+
				value.targetId+'" style="color:blue">'+value.targetName+'<a> </p><p class="minor" style="margin-top: 18px;">'+
				value.time+'</p> </div>');
		}		
	});
	}
	$('#all_pages').html('');
	if(data.page.totalPage >=1){
		for(i=1;i<=data.page.totalPage;i++){
		if(i == data.page.pageNow){
			$('#all_pages').append('<a class="p_num p_curpage">'+i+'</a>');
		}else{
			$('#all_pages').append('<a href="javascript:getAjaxData('+i+');" class="p_num">'+i+'</a>');
		}
	}
	if(data.page.hasNext == 1){
		$('#all_pages').append('<a href="javascript:getAjaxData('+(data.page.pageNow+1)+');" class="p_num">下一页</a>');
	}
	$('#all_pages').append('<span>(第'+data.page.pageNow+'页)</span>');
	}
}

function setMyListData(data){
	$('.pool5 > table > tbody').html('');
	$.each(data.recentUploadMusic, function(index, value) {
				$(".pool5 > table > tbody").append(
						'<tr path="'+value.targetName +'" source="'+value.sourceImage+'"><td class="song_name"><a href="/ShareMusicProject/music.action?id='+
						value.sourceId +'" title="Clumsy">'+value.sourceName+'</a></td>' +
						'<td class="track_time">'+value.time+'</td>' + '<td class="song_act"><div class="song_do" style="width:170px;_width:180px;"><a class="song_play" href="javascript:playMusic(\''+value.targetName+'\');"  title="试听" ><span>试听</span></a>' + 
						'<a class="song_toclt" href="javascript:void(0)" title="添加到精选集" "><span>添加到精选集</span></a>﻿'+ '</div> </td><td class="private_act"><a class="removeIT" href="javascript:void(0)" title="" onclick="removesong(2335400,1340526392);">删除</a></td></tr>');
			});
	$('div[class="fav_artist"]>ul[class="clearfix"]').html('');
	$.each(data.playList, function(index, value) {
				$('div[class="fav_artist"]>ul[class="clearfix"]').append(
						'<li><a href="/ShareMusicProject/playList?id='+value.sourceId+'" title="'+value.sourceName+'"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="'+value.sourceImage+'" height="55" width="55" alt="'+value.sourceName+'" style="z-index: 1; "></a></li>');
			});
	$('div[class="chart_artsit"] > ul').html('');
	$.each(data.fanRank, function(index, value) {
				$('div[class="chart_artsit"] > ul').append(
						'<li><div class="chart_artist_item"><a class="ico_radio" href="/ShareMusicProject/home.action?id='+value.sourceId+'" title="">'+value.sourceName+'</a>'
								+ '<a class="buddy" href="/ShareMusicProject/home.action?id='+value.sourceId+'" title=""><img src="'+
								value.sourceImage+'" width="55" height="55" alt=""></a><em class="playcounts">'+
								value.number+'</em><div class="stat_bar clearfix" style="width: '+(10/(index+1))*8+'%"></div></div></li>');
			});
	$('div[class="chart_song clearfix"] > ul').html('');
	$.each(data.musicListeningRank, function(index, value) {
				$('div[class="chart_song clearfix"] > ul').append(
						'<li class="no_'+(index+1)+'"><div class="chart_song_item"><em class="playcounts">'+value.number+'</em><div class="stat_bar" style="width: '+
						(10/(index+1))*8+'%"><span class="rank">0'+(index+1)+'</span> <span class="name"><a href="/ShareMusicProject/music.action?id='+value.sourceId+'" title="">'+value.sourceName+'</a></span></div></li>');
			});
}

function setMessageContent(data){
	$('#wall_list > ul').html('');
	if(data.page.totalSize > 0){
		$('p[class="wall_list_count"]').html('<span>'+data.page.totalSize+'</span>条留言');
		$.each(data.message, function(index, value) {
			$('#wall_list > ul').append('<li id="'+value.sourceId+'"><div class="post_item"><p class="usr_cover"><a class="personalDropDown" href="/ShareMusicProject/home.action?id='+value.sourceId+'" title="Kirishima.UK"> <img src="'+value.sourceImage+'" width="50" height="50" alt="Kirishima.UK"> </a></p><div class="info"> <span class="author"><a href="/ShareMusicProject/home.action?id='+
			value.sourceId+'" title="Kirishima.UK">'+value.sourceName+'</a></span><span class="time">'+value.time+'</span></div><div class="brief"><div id="brief_11061724">'+value.content+'</div></div></div></li>');
		});
		if(data.page.totalPage >=1){
			for(i=1;i<=data.page.totalPage;i++){
			if(i == data.page.pageNow){
				$('#all_message_pages').append('<a class="p_num p_curpage">'+i+'</a>');
			}else{
				$('#all_message_pages').append('<a href="javascript:getMessageData('+i+');" class="p_num">'+i+'</a>');
			}
		}
		if(data.page.hasNext == 1){
			$('#all_message_pages').append('<a href="javascript:getMessageData('+(data.page.pageNow+1)+');" class="p_num">下一页</a>');
		}
		$('#all_message_pages').append('<span>(第'+data.page.pageNow+'页)</span>');
		}
	}
}
function setMyMessageContent(data){
	$('#my_wall_list > ul').html('');
	if(data.page.totalSize > 0){
		$('p[class="wall_list_count"]').html('<span>'+data.page.totalSize+'</span>条留言');
		$.each(data.message, function(index, value) {
			$('#my_wall_list > ul').append('<li id="'+value.sourceId+'"><div class="post_item"><p class="usr_cover"><a class="personalDropDown" href="/ShareMusicProject/home.action?id='+value.sourceId+'" title="Kirishima.UK"> <img src="'+value.sourceImage+'" width="50" height="50" alt="Kirishima.UK"> </a></p><div class="info"> <span class="author"><a href="/ShareMusicProject/home.action?id='+
			value.sourceId+'" title="Kirishima.UK">'+value.sourceName+'</a></span><span class="time">'+value.time+'</span></div><div class="brief"><div id="brief_11061724">'+value.content+'</div></div></div></li>');
		});
		$('#all_message_pages').html('');
		if(data.page.totalPage >=1){
			for(i=1;i<=data.page.totalPage;i++){
			if(i == data.page.pageNow){
				$('#all_message_pages').append('<a class="p_num p_curpage">'+i+'</a>');
			}else{
				$('#all_message_pages').append('<a href="javascript:getMessageData('+i+');" class="p_num">'+i+'</a>');
			}
		}
		if(data.page.hasNext == 1){
			$('#all_message_pages').append('<a href="javascript:getMessageData('+(data.page.pageNow+1)+');" class="p_num">下一页</a>');
		}
		$('#all_message_pages').append('<span>(第'+data.page.pageNow+'页)</span>');
		}
	}
}

//$("#p_news").click(function() { getMyNews(); });
function getMyNews(){
	isNavSelecting("#my_news","#p_news");
	isNavNotSelect("#friend_news","#p_index");
	isNavNotSelect("#my_lists","#p_list");
	isNavNotSelect("#my_sharemusic","#p_share");
	getListContent();
	getMessageData(1);
}

function getMyIndex(){
	isNavNotSelect("#my_news","#p_news");
	isNavSelecting("#friend_news","#p_index");
	isNavNotSelect("#my_lists","#p_list");
	isNavNotSelect("#my_sharemusic","#p_share");
	isNavNotSelect("#user_messages","#p_messages");
}

function getMyList(){
	isNavNotSelect("#my_news","#p_news");
	isNavSelecting("#my_lists","#p_list");
	isNavNotSelect("#my_sharemusic","#p_share");
	isNavNotSelect("#friend_news","#p_index");
	
}

function getUserMessages(){
	isNavSelecting("#user_messages","#p_messages");
	isNavNotSelect("#friend_news","#p_index");
	getMessageData(1);
}

function shareMusic(){
	isNavNotSelect("#my_news","#p_news");
	isNavNotSelect("#my_lists","#p_list");
	isNavSelecting("#my_sharemusic","#p_share");
	isNavNotSelect("#friend_news","#p_index");
}

function isNavSelecting(id1,id2){
	$(id1).show();
	$(id2).css("background","white");
}

function isNavNotSelect(id1,id2){
	$(id1).hide();
	$(id2).css("background","url(http://img.xiami.com/res/img/default/pro_navi_bg.gif) top right no-repeat #E4E4E4");
}

function playMusic(src){
	console.log(src);
	            // Check for audio element support.
        if (window.HTMLAudioElement) {
            try {
                var oAudio = document.getElementById('mAudio');
                //console.log(oAudio.src);
                // Tests the paused attribute and set state. 
                if (oAudio.paused) {
                	oAudio.src = "/ShareMusicProject" + src;
                    oAudio.play();
                    //document.getElementById('playIndicator').innerHTML = '立刻暂停';
                }
                else {
                    oAudio.pause();
                    //document.getElementById('playIndicator').innerHTML = '立刻播放';
                }
            }
            catch (e) {
                // Fail silently but show in F12 developer tools console
                 if(window.console && console.error("Error:" + e));
            }
        }
}

function playMusic2(src,o){
	console.log(src);
	            // Check for audio element support.
        if (window.HTMLAudioElement) {
            try {
                var oAudio = document.getElementById('mAudio');
                //console.log(oAudio.src);
                // Tests the paused attribute and set state. 
                if (oAudio.paused) {
                	oAudio.src = "/ShareMusicProject" + src;
                    oAudio.play();
					o.src = "my_profile_files/pause.png"
                    //document.getElementById('playIndicator').innerHTML = '立刻暂停';
                }
                else {
                    oAudio.pause();
                    o.src = "my_profile_files/play.png"
                    //document.getElementById('playIndicator').innerHTML = '立刻播放';
                }
            }
            catch (e) {
                // Fail silently but show in F12 developer tools console
                 if(window.console && console.error("Error:" + e));
            }
        }
}

function addMessage(){
	uId = $('#messageUID')[0].value;
	value = $('#message_short').val();
	$.ajax( {
		url : "getComments!message.action",
		type : "POST",
		data : {
			userId:uId,
			content:value
		},
		dataType : "json",
		success : function(data){
			getUserMessages(1);
		}
	});	
}

function playsongs(){
	window.open("musicPlayer.htm", "newwindow", "height=355, width=630, top=0,left=0,toolbar=no, menubar=no");
}